#include "mainui.h"
#include "ui_mainui.h"
#include "hisdataui.h"
#include "systemui.h"
#include "paramui.h"
#include "constants.h"
#include <QTimer>
#include <QDateTime>

extern int MAJORVER;
extern int MINORVER;
extern int REVIVER;

MainUI::MainUI(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MainUI)
{
    ui->setupUi(this);
    setWindowFlags(this->windowFlags() | Qt::FramelessWindowHint );
    //load logo and background
    QPixmap lg(logoStr);
    ui->logo->setPixmap(lg);
    QPixmap bg(backgroundStr);
    QPalette background;
    background.setBrush(QPalette::Background, QBrush(bg));
    setPalette(background);
    //set font color
    QPalette pe;
    pe.setColor(QPalette::WindowText,Qt::white);
    ui->cTime->setPalette(pe);
    ui->lVer->setPalette(pe);
    ui->title->setPalette(pe);
    ui->his->setPalette(pe);
    ui->exp->setPalette(pe);
    ui->set->setPalette(pe);
    //button icon
    ui->btnExp->setIcon(QIcon(expStr));
    ui->btnExp->setIconSize(QSize(155, 160));
    ui->btnExp->setStyleSheet("background-color: transparent;");
    ui->btnSys->setIcon(QIcon(sysStr));
    ui->btnSys->setIconSize(QSize(120, 125));
    ui->btnSys->setStyleSheet("background-color: transparent;");
    ui->btnHis->setIcon(QIcon(hisStr));
    ui->btnHis->setIconSize(QSize(120, 125));
    ui->btnHis->setStyleSheet("background-color: transparent;");

    QTimer *upTime = new QTimer(this);
    connect(upTime,SIGNAL(timeout()),this,SLOT(updateTime()));
    updateTime();
    upTime->start(1000);

    QString qVer = QString("%1 %2.%3.%4").arg(tr("Version: ")).arg(MAJORVER).arg(MINORVER).arg(REVIVER);
    ui->lVer->setText(qVer);
    ui->title->setFocus();
}

MainUI::~MainUI()
{
    delete ui;
}

void MainUI::getShow()
{
    this->show();
    this->activateWindow();
    this->raise();
}

void MainUI::updateTime()
{
    QDateTime curTime = QDateTime::currentDateTime();
    QString lblTm = curTime.date().toString("yyyy/MM/dd") + "\n " + curTime.time().toString("hh:mm:ss");
    this->ui->cTime->setText(lblTm);
}

void MainUI::on_btnHis_clicked()
{
    this->hide();
    emit sendHis();
}

void MainUI::on_btnExp_clicked()
{
    this->hide();
    emit sendPar();
}

void MainUI::on_btnSys_clicked()
{
    this->hide();
    emit sendSys();
}
